package com.example.customviewtest.clipImage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.customviewtest.R;

/**
 * @auther XuDan on 2020/4/22.
 */
public class ClipRectangleView extends View {
    private Context context;
    private Paint mPaint;
    private String imgPath;
    private Paint mBmpPaint;
    private int mWidth;
    private int mHeight;
    private int mLeft;
    private int mRight;
    private int mTop;
    private int mBottom;
    private Bitmap mReSize_bitmap;

    public static int clip_height = 800;
    public static int clip_width = 800;
    public static int corner_length = 50;


    public ClipRectangleView(Context context) {
        this(context, null);
    }

    public ClipRectangleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClipRectangleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mPaint = new Paint();
        mBmpPaint = new Paint();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ClipRectangleView);
        int ratio = typedArray.getInt(R.styleable.ClipRectangleView_ration, 1);
        switch (ratio) {
            case 1:
                clip_width = 800;
                clip_height = 800;
                break;
            case 2:
                clip_width = 800;
                clip_height = 800 * 3 / 4;
                break;
            case 3:
                clip_width = 800;
                clip_height = 800 * 4 / 3;
                break;
        }


        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        int mHalf_width = mWidth / 2;
        int mHalf_height = mHeight / 2;
        mLeft = mHalf_width - clip_width / 2;
        mRight = mHalf_width + clip_width / 2;
        mTop = mHalf_height - clip_height / 2;
        mBottom = mHalf_height + clip_height / 2;
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
        mReSize_bitmap = reSize(bitmap);
        canvas.drawBitmap(mReSize_bitmap, 0, 0, mBmpPaint);
        drawGridding(canvas);
    }

    private Bitmap reSize(Bitmap bitmap) {
        int toWidth = mWidth, toHeight = mHeight;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int resizeWidth, resizeHeight, x, y;
        float rate = (float) (1.0 * toWidth / toHeight);
        if (1.0 * width / height == rate)
            return bitmap;
        if (1.0 * width / height > rate) {
            resizeHeight = height;
            resizeWidth = (int) (height * rate);
            x = (width - resizeWidth) / 2;
            y = 0;
        } else {
            resizeWidth = width;
            resizeHeight = (int) (width / rate);
            x = 0;
            y = (height - resizeHeight) / 2;
        }
        Bitmap b = Bitmap.createBitmap(bitmap, x, y, resizeWidth, resizeHeight);
        if (resizeHeight == toHeight) {
            return b;
        } else {
            Matrix matrix = new Matrix();
            matrix.preScale((float) 1.0 * toWidth / resizeWidth, (float) 1.0 * toHeight / resizeHeight);
            return Bitmap.createBitmap(b, 0, 0, resizeWidth, resizeHeight, matrix, false);
        }
    }


    private void drawGridding(Canvas canvas) {
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.STROKE);
        int horizontal_length = clip_width / 3;
        int vertical_length = clip_height / 3;
        canvas.drawRect(mLeft, mTop, mRight, mBottom, mPaint);
        for (int i = 1; i < 3; i++) {
            canvas.drawLine(mLeft + i * horizontal_length, mTop, mLeft + i * horizontal_length, mBottom, mPaint);
        }
        for (int i = 1; i < 3; i++) {
            canvas.drawLine(mLeft, mTop + i * vertical_length, mRight, mTop + i * vertical_length, mPaint);
        }
        mPaint.setStrokeWidth(15);
        mPaint.setColor(Color.WHITE);
        canvas.drawLine(mLeft, mTop, mLeft + corner_length, mTop, mPaint);
        canvas.drawLine(mLeft, mBottom, mLeft + corner_length, mBottom, mPaint);

        canvas.drawLine(mRight - corner_length, mTop, mRight, mTop, mPaint);
        canvas.drawLine(mRight - corner_length, mBottom, mRight, mBottom, mPaint);

        canvas.drawLine(mLeft, mTop, mLeft, mTop + corner_length, mPaint);
        canvas.drawLine(mRight, mTop, mRight, mTop + corner_length, mPaint);

        canvas.drawLine(mLeft, mBottom - corner_length, mLeft, mBottom, mPaint);
        canvas.drawLine(mRight, mBottom - corner_length, mRight, mBottom, mPaint);
    }

    int dx = 0;
    int dy = 0;
    float last_y = 0;
    float last_x = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                last_x = event.getRawX();
                last_y = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                dx = (int) (event.getRawX() - last_x);
                dy = (int) (event.getRawY() - last_y);
                mLeft = mLeft + dx;
                mRight = mRight + dx;
                mTop = mTop + dy;
                mBottom = mBottom + dy;
                if (mLeft < 0) {
                    mLeft = 0;
                    mRight = clip_width;
                }
                if (mTop < 0) {
                    mTop = 0;
                    mBottom = clip_height;
                }
                if (mRight > mWidth) {
                    mRight = mWidth;
                    mLeft = mRight - clip_width;
                }
                if (mBottom > mHeight) {
                    mBottom = mHeight;
                    mTop = mBottom - clip_height;
                }
                invalidate();
                last_x = event.getRawX();
                last_y = event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    public void setImagePath(String path) {
        this.imgPath = path;
        invalidate();
    }

    public Bitmap getClipBitmap() {
        Bitmap bitmap = Bitmap.createBitmap(mReSize_bitmap, mLeft, mTop, clip_width, clip_height);
        return bitmap;

    }

    public void recycle() {
        if (mReSize_bitmap != null && !mReSize_bitmap.isRecycled()) {
            mReSize_bitmap.recycle();
        }
    }
}
